from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.conf import settings
from paramscontrol import process_form_inputs
from models import MetricSite, MetricSet, MetricDefn, MetricStat
from utils import get_all_defn_stats

@login_required
def main(request):
    
    vars_dict = process_form_inputs(request.GET)
    
    is_configured = vars_dict['is_configured']
    has_data = vars_dict['has_data']
    
    if not is_configured:
        return render_to_response('notconfigured.html', locals())
    
    metric_site = vars_dict['metric_site'] #get_selected_metric_site(getattr(form, 'cleaned_data', {'sitename': None})['sitename'])
    metric_set = vars_dict['metric_set'] #get_selected_metric_set(getattr(form, 'cleaned_data', {'setname': None})['setname'])

    all_metric_sitenames = vars_dict['all_metric_sitenames']
    all_metric_setnames = vars_dict['all_metric_setnames']

    use_table = vars_dict['use_table']
    all_times = vars_dict['all_times']
    date_choices = vars_dict['date_choices']
    date_choices_with_all = vars_dict['date_choices_with_all']
    
    start_date_str = vars_dict['start_date_str']
    end_date_str = vars_dict['end_date_str']

    defns = metric_set.metricdefn_set.all()

    # Indexed by defn, values are arrays of values in same order as all_times
    stats_table = get_all_defn_stats(defns, all_times)
     
    # Path to amline
    amline_path = getattr(settings, 'METRICS_AMLINE_PREFIX', '/amline/')
    
    return render_to_response('graph.html', locals())

